// Copyright 2011 Jay Young. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview
 *
 * Provides a Command object class for updating the user's display name.
 */

goog.provide('rb.auth.UpdateUserCommand');

goog.require('goog.json');

goog.require('relief.rpc.Command');



/**
 * A Command subclass for updating a user's record on the server with a new
 * display name.
 *
 * @param {function()} onSuccess Callback for a successful server trip.
 * @param {function()} onFailure Callback for a failed server trip.
 * @param {string} name The user's new display name.
 * @param {string} userId The user's unique ID.
 *
 * @constructor
 * @extends {relief.rpc.Command}
 */
rb.auth.UpdateUserCommand = function(onSuccess, onFailure, name, userId) {
  goog.base(this, onSuccess, onFailure, 'UpdateUserCmd:' + name + ':' + userId,
            '/api/auth/update', 'POST');

  /**
   * @type {string}
   * @private
   */
  this.name_ = name;

  /**
   * @type {string}
   * @private
   */
  this.userId_ = userId;
};
goog.inherits(rb.auth.UpdateUserCommand, relief.rpc.Command);


/**
 * @return {string} The JSON-serialized POST content.
 * @override
 */
rb.auth.UpdateUserCommand.prototype.getData = function() {
  return goog.json.serialize({
    'userId': this.userId_,
    'displayName': this.name_
  });
};


/**
 * @inheritDoc
 */
rb.auth.UpdateUserCommand.prototype.onSuccess = function(e) {
  this.callersOnSuccess();
};


/**
 * @inheritDoc
 */
rb.auth.UpdateUserCommand.prototype.onFailure = function(e) {
  this.callersOnFailure();
};
